package com.example.flutter_project

import android.os.Bundle
import android.os.PersistableBundle
import com.volcengine.tos.TOSV2ClientBuilder
import com.volcengine.tos.TosException
import com.volcengine.tos.model.`object`.PutObjectBasicInput
import com.volcengine.tos.model.`object`.PutObjectFromFileInput
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel


class MainActivity : FlutterActivity() {
    private companion object {
        @JvmStatic
        private val CHANNEL_METHOD = "initTOSSDK"
    }

    private lateinit var uploadMediaChannel: MethodChannel

    override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
        super.onCreate(savedInstanceState, persistentState)

    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        initMethodChannel()
        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger, CHANNEL_METHOD
        ).setMethodCallHandler { methodCall, result ->
            when (methodCall.method) {
                "uploadImg" -> {
                    var message = methodCall.arguments;
                    var list = message as ArrayList<String>;
                    uploadToTOS(list);
                    result.success(true)
                }
            }
        }
    }


    private fun uploadToTOS(messageList: ArrayList<String>) {
        val endpoint = "tos-cn-beijing.volces.com"
        val region = "cn-beijing"
        var filePath = messageList[0]
        val accessKey = messageList[1]
        val secretKey = messageList[2]
        val secretToken = messageList[3]
        val type = messageList.last();
        var imgNameList = filePath.split('/') as ArrayList<String>;
        val bucketName = "pixverse-internal-upload"
        println("开始上传")
        // 对象名，模拟 example_dir 下的 example_object.txt 文件
        val objectKey = "platform/$type/upload/${imgNameList.last()}"
        // 本地文件路径，请保证文件存在，暂不支持文件夹功能
        val tosThread = Thread {
            try {
                val tos =
                    TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey, secretToken)
                println(tos.toString() + "返回的数据")
                val basicInput = PutObjectBasicInput().setBucket(bucketName).setKey(objectKey)
                val putObjectInput = PutObjectFromFileInput().setPutObjectBasicInput(basicInput)
                    .setFilePath(filePath)
                val output = tos.putObjectFromFile(putObjectInput)
                println(
                    "putObject succeed, object's etag is " + output.putObjectOutput.etag
                )
                println(
                    "putObject succeed, object's crc64 is " + output.putObjectOutput.hashCrc64ecma
                )
                runOnUiThread {
                    uploadMediaChannel.invokeMethod(
                        "upload_media_file",
                        output.putObjectOutput.etag
                    )
                }

            } catch (e: TosException) {
                println("putObjectFromFile failed")
                e.printStackTrace()
            }
        }

        tosThread.start()
    }

    private fun initMethodChannel() {
        uploadMediaChannel =
            MethodChannel(flutterEngine?.dartExecutor!!.binaryMessenger, "uploadMedia")
    }
}
